<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseUserController.php';
require_once 'CMS/UserCommon.php';
require_once 'CMS/Language.php'; //include thu vien language

//Người tạo : Nguyễn Hồng Danh - Team 2
//Người chỉnh sửa: Nguyễn Huỳnh Đại Thành - Team 3
//Module 	: Edit MostViewQA Module
//Chức năng	: Quản lý việc Edit các thuộc tính cho  module MostViewQA

class MostViewQA_ClickchangeController extends BaseUserController{

	public function init(){
		parent::init();
		$this->_helper->layout->disableLayout();
	}
	
	//Chức năng show các thuộc tính hiện tại cho người dùng
	public function indexAction(){
		
		/*========== Start Load Language ==========*/
		$language = new Language(APPLICATION_PATH . '/widgets/mostviewnews');
		if(isset($_SESSION['language']))
		{
			$language_id = $_SESSION['language'];
			$tbLanguage = new Language_Model_Language();
			$name = $tbLanguage->getNameLanguage($language_id);
		}
		else
			$name['name'] = 'Default';
		$language->load($name['name']);
		/*========== End Load Language ==========*/
			
		/*========== Start Get Text ==============*/
		$this->view->textTitle 		= $language->get('text_title');
		$this->view->textOrder 		= $language->get('text_order');
		$this->view->textSave 		= $language->get('text_save');
		$this->view->textLimit 		= $language->get('text_limit');
		$this->view->textError1 	= $language->get('error1');
		$this->view->textError2 	= $language->get('error2');
		
		$this->view->textAlert1 	= $language->get('alert1');
		$this->view->textAlert2 	= $language->get('alert2');
		$this->view->textAlert3 	= $language->get('alert3');
		$this->view->textAlert4 	= $language->get('alert4');
		$this->view->textAlert5 	= $language->get('alert5');
		$this->view->textAlert6 	= $language->get('alert6');
		$this->view->textAlert7 	= $language->get('alert7');
		
		$this->view->language 	= $name['name'];
		/*========== End Get Text ==============*/
		
		//Start ------- Khởi tạo Parameter
		if(isset($_GET["url"]))
		{
			$this->view->url = $_GET["url"];
		}
		$path = APPLICATION_PATH."/widgets/mostviewqa/";
		$tbSidebarModule = new  MostViewQA_Model_SidebarModule();
		//End ------- Khởi tạo Parameter
		
		//Start ------- Kiểm Tra file xml và truyền biến qua view
		$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
		$user = new User();
			$user->init();
			$user->check_login();
			$hasmodify = $user->modifiy_widget($xmlconfig->name);
			if($hasmodify=="true")
	{
		if(isset($xmlconfig->name))
		{
			$module_MostView = $tbSidebarModule->selectModuleByCode($xmlconfig->name);
			if(count($module_MostView)>0)
			{
				$this->view->sort = $module_MostView[0]["sort_order"];
				$this->view->name = $xmlconfig->name;
				$titleXML = 'title'.$name['name'];
				if(isset($xmlconfig->$titleXML))
				{
					$this->view->title = $xmlconfig->$titleXML;
				}
				else
				{
					if(isset($xmlconfig->titleDefault))
					{
		        		$this->view->title = $xmlconfig->titleDefault;
					}
		        	else
		        	{
		        		$this->view->error = 1 ;
		        	}
				}
				if(isset($xmlconfig->limit))
				{
					$this->view->limit = "".$xmlconfig->limit;
				}
				else 
				{
					$this->view->error = 1 ;
				}
				
			}
			else
			{
				$this->view->error = 1 ;
			}
		}
		else
		{
			$this->view->error = 1 ;
		}
		//End ------- Kiểm Tra file xml và truyền biến qua view
	}
	else
	{
		$this->view->error = 2 ;
	}
	}
	
	//Chức năng Edit lại các thuộc tính mà người dùng đã chọn
	public function editAction(){
			if(isset($_GET["name"]))
			{
				$tbSidebarModule = new  MostViewQA_Model_SidebarModule();
				$module_MostView  = $tbSidebarModule->selectModuleByCode($_GET["name"]);
				if(count($module_MostView)>0)
				{
					if(isset($_GET["title"])&&(strlen($_GET["title"])>=5))
					{
						if(isset($_GET["limit"])&&(is_numeric($_GET["limit"])))
						{
							if($_GET["limit"]>0)
							{
								if(isset($_GET["sort"])&&(is_numeric($_GET["sort"])))
								{
									if($_GET["sort"]>=0)
									{
										if($module_MostView[0]["sort_order"] == $_GET["sort"])
										{
											$modules = 0;
										}
										else 
										{
											$modules = $tbSidebarModule->selectModuleBySortOrder($_GET["sort"]);
										}
										if(($modules==0)||count($modules)<=0)
										{
											$tbSidebarModule->updateModule($_GET["name"],$_GET["sort"]);//Save Sort_Order in DB
											
											//Start ------- Save title and limit in xml file
											$path = APPLICATION_PATH."/widgets/mostviewqa/";
											$xmlconfig = new SimpleXMLElement($path."config.xml",null,true);//Read config.xml
											$titleXML = 'title'.$_GET['language'];
											$xmlconfig->$titleXML = $_GET['title'];
											$xmlconfig->limit = $_GET['limit'];
											file_put_contents($path."config.xml", $xmlconfig->asXML());
											//End ------- Save title and limit in xml file
											
											$data = 1;	
											
										}
										else
										{
											$data = -5;
										}
									}
									else 
									{
										$data = -6;
									}
								}
								else
								{
									$data = -4;
								}
							}
							else 
							{
								$data = -7;
							}
						}
						else 
						{
							$data = -3;
						}
					}
					else 
					{
						$data = -2;
					}
				}
				else
				{
					$data = -1;
				}
			}
			else 
			{
				$data = -1;
			}
			$this->_helper->json($data); 
	}
}